<template>
  <div>
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="2"
        :formType="type"
        labelWidth="110"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <AjaxButton :click="saveDialog" type="primary">保 存</AjaxButton>
    </div>
  </div>
</template>

<script>
import { addTestDriveCars, editTestDriveCars, getBrandList, getSeriesList, getModelList, getPackageList } from '@/api/potenCus/basedata/searchTestDriveCars';
import { dictionary } from '@/utils/dictMixin';
import { IDNumber, phone, checkVehicleMark, VIN } from '@/utils/validate';

export default {
  name: 'ChannelModal',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList()
    };
  },
  created() {
    this.init();
    this.getDictBrandList();
  },
  methods: {
    async init() {
      console.log('this.rowData', this.rowData);
      console.log('this.modelId', this.rowData.modelId);
      if (this.type === 'edit') {
        // 更新接口需要的记录 ID
        this.recordId = this.rowData.testDriveId;
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
        // this.changeBrandSelect(this.rowData.brandId);
        const res1 = await getSeriesList(this.rowData.brandId);
        if (res1.resultCode === 200) {
          this.formList.find(item => item.fieldName === 'seriesId').itemList = res1.data.map(x => ({ value: x.seriesId, text: x.seriesName }));
        }
        // this.changeSeriesSelect(this.rowData.seriesId);
        const res2 = await getModelList(this.rowData.brandId, this.rowData.seriesId);
        if (res2.resultCode === 200) {
          this.formList.find(item => item.fieldName === 'modelId').itemList = res2.data.map(x => ({ value: x.modelId, text: x.modelName }));
        }
        // this.changeModelSelect(this.rowData.modelId);
        const res3 = await getPackageList(this.rowData.modelId, this.rowData.brandId, this.rowData.seriesId);
        if (res3.resultCode === 200) {
          this.formList.find(item => item.fieldName === 'packageId').itemList = res3.data.map(x => ({ value: x.packageId, text: x.configName }));
        }
        this.formList.find(item => item.fieldName === 'brandId').initialValue = Number(this.rowData.brandId);
        this.formList.find(item => item.fieldName === 'seriesId').initialValue = Number(this.rowData.seriesId);
        this.formList.find(item => item.fieldName === 'modelId').initialValue = Number(this.rowData.modelId);
        this.formList.find(item => item.fieldName === 'packageId').initialValue = Number(this.rowData.packageId);
      }
    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '品牌',
          fieldName: 'brandId',
          placeholder: '请选择品牌',
          disabled: this.type === 'edit',
          itemList: [],
          rules: [
            { required: true, message: '请选择品牌', trigger: 'blur' },
            { required: true, message: '请选择品牌', trigger: 'blur' }
          ],
          change: val => {
            this.changeBrandSelect(val);
          }
        },
        {
          type: 'SELECT',
          label: '车系',
          fieldName: 'seriesId',
          placeholder: '请选择车系',
          disabled: this.type === 'edit',
          itemList: [],
          rules: [
            { required: true, message: '请选择车系', trigger: 'blur' },
            { required: true, message: '请选择车系', trigger: 'blur' }
          ],
          change: val => {
            this.changeSeriesSelect(val);
          }
        },
        {
          type: 'SELECT',
          label: '车型',
          fieldName: 'modelId',
          placeholder: '请选择车型',
          disabled: this.type === 'edit',
          itemList: [],
          rules: [
            { required: true, message: '请选择车型', trigger: 'blur' },
            { required: true, message: '请选择车型', trigger: 'blur' }
          ],
          change: val => {
            this.changeModelSelect(val);
          }
        },
        {
          type: 'SELECT',
          label: '配置',
          fieldName: 'packageId',
          placeholder: '请选择配置',
          disabled: this.type === 'edit',
          itemList: [],
          rules: [
            { required: true, message: '请选择配置', trigger: 'blur' },
            { required: true, message: '请选择配置', trigger: 'blur' }
          ]
        },
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'vehicleMark',
          placeholder: '请输入车牌号',
          maxlength: 10,
          rules: [
            { required: true, message: '请输入车牌号', trigger: 'blur' },
            { required: true, trigger: 'blur', validator: checkVehicleMark }
          ]
        },
        {
          type: 'INPUT',
          label: 'VIN号',
          fieldName: 'vin',
          placeholder: '请输入VIN',
          disabled: this.type === 'edit',
          rules: [
            { required: true, message: '请输入VIN', trigger: 'blur' },
            { required: true, validator: VIN, trigger: 'blur' }
          ]
        },
        {
          type: 'INPUT_NUMBER',
          label: '总里程',
          fieldName: 'allMilliage',
          disabled: this.type === 'edit',
          initialValue: 0,
          placeholder: '请输入总里程',
          rules: [
            { required: true, message: '请输入总里程', trigger: 'blur' },
            { required: true, message: '请输入总里程', trigger: 'blur' }
          ]
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'isValid',
          placeholder: '请选择',
          initialValue: 10041001,
          itemList: this.createDictList('1004'),
          rules: [{ required: true, message: '请选择是否有效', trigger: 'blur' }]
        },
        {
          type: 'INPUT_NUMBER',
          label: '排序',
          fieldName: 'rank',
          placeholder: '请输入'
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        let res = {};
        if (this.type === 'add') {
          res = await addTestDriveCars(this.saveData);
        }
        if (this.type === 'edit') {
          res = await editTestDriveCars(this.saveData, this.recordId);
        }
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
          this.$message.success('保存成功!');
        }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },

    async getDictBrandList() {
      const res = await getBrandList();
      if (res.resultCode === 200) {
        this.formList[0].itemList = res.data.map(x => ({ value: x.brandId, text: x.brandName }));
        // this.topFilterList = [...this.topFilterList]
      }
    },
    async changeBrandSelect(brandsid) {
      if (!brandsid) {
        this.formList[1].initialValue = '';
        this.formList[1].itemList = [];
        this.formList[2].initialValue = '';
        this.formList[2].itemList = [];
        this.formList[3].initialValue = '';
        this.formList[3].itemList = [];
      } else {
        const res = await getSeriesList(brandsid);
        if (res.resultCode === 200) {
          this.formList[1].itemList = res.data.map(x => ({ value: x.seriesId, text: x.seriesName }));
        }
        if (this.formList[1].initialValue) {
          this.formList[1].initialValue = '';
          this.formList[2].initialValue = '';
          this.formList[3].initialValue = '';
        }
      }
    },
    async changeSeriesSelect(seriessid) {
      const brandsid = this.formList[0].initialValue;
      if (!brandsid || !seriessid) {
        this.formList[2].initialValue = '';
        this.formList[2].itemList = [];
        this.formList[3].initialValue = '';
        this.formList[3].itemList = [];
      } else {
        const res = await getModelList(brandsid, seriessid);
        if (res.resultCode === 200) {
          this.formList[2].itemList = res.data.map(x => ({ value: x.modelId, text: x.modelName }));
        }
        if (this.formList[2].initialValue) {
          this.formList[2].initialValue = '';
          this.formList[3].initialValue = '';
        }
      }
    },
    async changeModelSelect(modelId) {
      const brandsid = this.formList[0].initialValue;
      const seriessid = this.formList[1].initialValue;
      if (!brandsid || !seriessid || !modelId) {
        this.formList[3].initialValue = '';
        this.formList[3].itemList = [];
      } else {
        const res = await getPackageList(modelId);
        if (res.resultCode === 200) {
          this.formList[3].itemList = res.data.map(x => ({ value: x.packageId, text: x.configName }));
        }
        if (this.formList[3].initialValue) {
          this.formList[3].initialValue = '';
        }
      }
    }
  }
};
</script>

<style lang="less" scoped>
</style>
